package com.io.AIO;

import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

public class AsyncTimeServerHandler implements Runnable{
	private int port;
	CountDownLatch latch;
	AsynchronousServerSocketChannel asynchronousServerSocketChannel;
	public AsyncTimeServerHandler(int port) {
		this.port = port;
		try {
			asynchronousServerSocketChannel =AsynchronousServerSocketChannel.open();
			asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
			System.out.println("The time server is start in port: "+port);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	@Override
	public void run() {
		latch =new CountDownLatch(1);
		doAccept();
		try {
			latch.countDown();
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	public void doAccept() {
		asynchronousServerSocketChannel.accept(this,new AcceptCompletionHandler());
	}
}
